package com.fjec.market.controller;

import com.fjec.common.protocol.JsonResult;
import com.fjec.market.service.IInfoDeliveryAddressService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 009.商户收货地址表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/")
public class InfoDeliveryAddressController {

    @Resource
    private IInfoDeliveryAddressService service;

    /**
     * 增加获更新地址
     *
     * @param id       主键id
     * @param userId   用户Id
     * @param busiType 商户类型
     * @param phone    联系电话
     * @param contact  联系人
     * @param prov     省行政编码
     * @param city     市行政编码
     * @param district 区行政编码
     * @param address1 省市区文字
     * @param address2 详细地址
     * @return
     */
    @PostMapping("/y/market/addr/saveOrUpdate")
    public JsonResult saveOrUpdate(Long id, Long userId, Integer busiType, String phone,
                            String contact, String prov, String city, String district,
                            String address1, String address2) {
        if ( id == null || id == 0) {
            return service.saveDeliveryAddress(userId, busiType, phone, contact, prov, city, district, address1, address2, 0);
        } else {
            return service.updateDeliveryAddress(id, phone, contact, prov, city, district, address1, address2);
        }
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @PostMapping("/y/market/addr/del")
    public JsonResult deleteDeliveryAddress(Long id) {
        return service.deleteDeliveryAddress(id);
    }


    /**
     * 设置默认地址
     *
     * @param id
     * @return
     */
    @PostMapping("/y/market/addr/setdef")
    public JsonResult setDefaultDeliveryAddress(Long id) {
        return service.setDefaultDeliveryAddress(id);
    }

    /**
     * 设置默认地址
     *
     * @return
     */
    @PostMapping("/y/market/addr/getdef")
    public JsonResult getDefaultDeliveryAddress() throws Exception {
        return service.getDefaultDeliveryAddress();
    }

    /**
     * 地址列表
     *
     * @param userId
     * @param busiType
     * @return
     * @throws Exception
     */
    @GetMapping("/y/market/addr/list")
    public JsonResult getDeliveryAddressList(Long userId, Integer busiType) throws Exception {
        return service.getDeliveryAddressList(userId, busiType);
    }

    /**
     * 根据Id获取地址
     *
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/y/market/addr/get")
    public JsonResult getDeliveryAddress(Long id) throws Exception {
        return service.getDeliveryAddress(id);
    }

}
